<?php
namespace Ntxx\Controller;
use Think\Controller;
class TopicAdminController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	
	public function index(){
		$param = $_GET;
		foreach($param as $k=>$v){
			$param[$k] = iconv("gb2312","utf-8",$v);
			$param[$k] = str_replace("'","",$param[$k]);
		}
		$this->assign('param',$param);// 赋值数据集
		
		$map = M("Topic")->create($param);
		
		foreach($map as $k=>$v){
			if($k=='title'){
				$map[$k] = array('like',"%$v%");
			}
		}
		
		
		$page = M('Topic'); // 实例化User对象
		$count      = $page->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Bootstrap\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $page->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//dump($list);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function edit(){
		$id = I('get.id',0,'intval');
		$exam_type = M("group")->select();
		$category = M("category")->select();
		if($id){
			$r = M('Topic')->where(array('id'=>$id))->find();
			$this->assign('r',$r);
		}
        $this->assign('c',$category);
		$this->display();
	}
	
	public function submit(){
//		dump($_POST);
//		exit();
		$i = I('post.id',0,'intval');
		$A = M('Topic');
		$data = $A->field('id',true)->create();
		$data['content'] = $_POST['content'];
		if($i){
			$map = array('id'=>$i);
			$p = $A->where($map)->count();
			if(!$p) $this->error('数据错误');
			$r = $A->where($map)->data($data)->save();
//			dump($r);
//            exit();
			if($r){
				$this->success('修改成功',U('index'));
			}
			else{
				$this->error('修改失败');
			}
		}
		else{
			$r = $A->data($data)->add();
			if($r){
				$this->success('新增成功',U('index'));
			}
			else{
				$this->error('新增失败');
			}
		}
		//$this->display();
	}
	
	public function delete(){
		$id = check2ids(I('post.ids'));
		if(!$id){
			$this->error('没有删除任何项目');
		}
		$map = array(
			'id'=>array(
				'in',
				$id,
			),
		);
		$r = M('Topic')->where($map)->delete();
		if(!$r){
			$this->error('没有删除任何项目');
		}
		else{
			$this->success('删除成功');
		}
	}
}